package com.coolwen.controller;

import com.alibaba.fastjson.JSON;
import com.coolwen.entity.Challenge;
import com.coolwen.entity.User;
import com.coolwen.service.ChallengeService;
import com.coolwen.service.TeamService;
import com.coolwen.service.UserService;
import com.coolwen.service.UserTeamService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

/**
 * 约战记录控制层
 * Created by user on 2016-11-23.
 */
@Controller
@RequestMapping(value = {"/challenge/Challenge"})
public class ChallengeController {

    /**
     * 引入log4j,进行日志打印
     */
    private static final Logger LOGGER = Logger.getLogger(ChallengeController.class);

    /**
     * 引入User,Team,UserTeam,Challenge各自的service，方便调用其方法。
     */
    @Autowired
    UserService userService;

    @Autowired
    TeamService teamService;

    @Autowired
    UserTeamService userTeamService;

    @Autowired
    ChallengeService challengeService;


    /**
     * 设置跳转到约战接受界面
     *
     * @return
     */

    @RequestMapping(value = "/receive", method = RequestMethod.GET)
    public String Receive(Model model, HttpSession session) {
        /*通过User的session，读取teamId*/
        User user = (User) session.getAttribute("loginUser");
        int teamId = userTeamService.findUserTeamByUserID(user.getUserId()).getTeamId();
        /*获取当接受者的id等于自己teamId的约战记录，并且约战记录的当前状态码为3*/
        model.addAttribute("challenge", challengeService.findChallengeByReceiveId("3", teamId));
        /*获取当接受者拒绝对方的约战请求时的数据*/
        model.addAttribute("challengeAgainst", challengeService.findByAgreeState(3));
        return "challenge/challengeReceive";
    }

    /**
     * 定义同意按钮
     *
     * @param challengeId
     * @return
     */
    @RequestMapping(value = "/agree/{challengeId}")
    public String Agree(@PathVariable int challengeId) {
        /*当点击同意按钮后，更新当前约战记录的状态码*/
        challengeService.updateAgree(challengeId);
        challengeService.updateAgreeAndConfirm(challengeId);
        return "redirect:/challenge/Challenge/receive";
    }

    /**
     * 定义拒绝按钮
     *
     * @param challengeId
     * @return
     */
    @RequestMapping(value = "/against/{challengeId}")
    public String Against(@PathVariable int challengeId) {
        /*当点击拒绝按钮后，更新当前状态码*/
        challengeService.updateAgainst(challengeId);
        challengeService.updateRefuse(challengeId);
        return "redirect:/challenge/Challenge/receive";
    }

    /**
     * 设置跳转到约战发送界面
     *
     * @return
     */
    @RequestMapping(value = "/send", method = RequestMethod.GET)
    public String Send(Model model, HttpSession session, Challenge challenge) {
        /*通过user的session，读取teamId*/
        User user = (User) session.getAttribute("loginUser");
        int teamId = userTeamService.findUserTeamByUserID(user.getUserId()).getTeamId();
        /*获取当前符合要求且已发送的约战请求*/
        model.addAttribute("challenge", challengeService.findByConfirmState("3", teamId));
        model.addAttribute("challengeAway", challengeService.findChallengeByReceiveId("3", teamId));
        model.addAttribute("challengeRefuse", challengeService.findByConfirmState("4", teamId));
        return "challenge/challengeSend";
    }

    /**
     * 点击取消按钮，撤回已发送的约战记录
     *
     * @param challengeId
     * @return
     */
    @RequestMapping(value = "/{challengeId}")
    public String Refuse(@PathVariable int challengeId) {
        /*点击取消按钮，取消已发送的约战请求，并且改变状态码*/
        challengeService.updateIsConfrimById(challengeId);
        return "redirect:/challenge/Challenge/send";
    }

    /**
     * 设置跳转到约战确认界面
     *
     * @return
     */
    @RequestMapping(value = "/confirm", method = RequestMethod.GET)
    public String Confirm(Model model, HttpSession session, Challenge challenge) {
        /*通过user的session，读取teamId*/
        User user = (User) session.getAttribute("loginUser");
        int teamId = userTeamService.findUserTeamByUserID(user.getUserId()).getTeamId();
        /*获取符合要求的约战信息（状态为1，并且该约战消息的teamid等于自己队伍的teamid）*/
        model.addAttribute("challenge", challengeService.findByConfirmState("1", teamId));
        /*如果是客场，则通过此方法获取数据，（状态为1，并且该约战消息的ReceiveId等于自己队伍的teamId）*/
        model.addAttribute("challengeAway", challengeService.findChallengeByReceiveId("1", teamId));
        return "challenge/challengeConfirm";

    }

    /**
     * 点击结束比赛按钮跳转到结束界面
     *
     * @param challengeId
     * @return
     */

    @RequestMapping(value = "/end/{challengeId}")
    public String End(@PathVariable int challengeId) {
        /*点击结束比赛按钮，更改状态码，并跳转约战记录已结束界面*/
        challengeService.updateEnd(challengeId);
        challengeService.updateEndAndPresent(challengeId);
        return "redirect:/challenge/Challenge/list/over";

    }


}


